package cn.szzsi.business.service;

import cn.szzsi.core.kit.CommonUtil;
import cn.szzsi.model.BusinessBalance;
import com.jfinal.kit.StrKit;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 商户余额业务类
 */
public class BusinessBalanceService {
    public static final BusinessBalanceService service=new BusinessBalanceService();

    /**
     * 余额明细,若表里面不存在该会员,才会创建,否则直接更新,不执行此方法
     * @param agentId 商户id
     * @param balance 商户资金余额
     * @param cashing 商户提现金额
     */
    public void saveInfo(String agentId, BigDecimal balance, BigDecimal cashing){
        BusinessBalance businessBalance=new BusinessBalance();
        businessBalance.setId(StrKit.getRandomUUID());
        businessBalance.setBid(agentId);
        businessBalance.setBalance(balance);
        businessBalance.setCashingMoney(cashing);
        businessBalance.setCreateDate(new Date());
        businessBalance.save();
    }

    /**
     *  更新余额
     * @param bid 商户id
     * @param balance 余额
     */
    public void updateBalance(String bid, BigDecimal balance){
        //1.查看结果表是否存在该会员信息
        BusinessBalance agentBalance= BusinessBalance.dao.getByBusinessId(bid);
        if(agentBalance!=null){//若存在,则直接更新
            agentBalance.setBalance(agentBalance.getBalance().add(balance));
            agentBalance.setUpdateDate(new Date());
            agentBalance.update();
        }else{//2.若不存在,则新增
            saveInfo(bid,balance,new BigDecimal(0));
        }
    }
}
